let app = getApp();
Page({
  data: {
    countList:[{fix:[150,100], animation:null}, {fix:[160,230], animation:null},
      {fix:[140,170], animation:null}, {fix:[190,40], animation:null}, {fix:[190,280], animation:null}],
    shareList:[],
    getList:[],
    getRecord:[],
    show:false,
    show2:false,
    gzShow:false,
    recordShow:false,
    userInfo:{nickname:'...'},
    shuidicount:0,
    onShow:false,
    guize:'',
    showQRCodePopup:false,
    conutClickShow:true
  },
  onLoad: function (options) {
    this.setData({options})
    this.getInt(options)

  },
  onShow(){
    let t = this
    wx.showLoading({title: '加载中',mask:true})
    app.checkUserPhone()
    t.data.onShow ? t.getInt({}) : t.setData({onShow:true});
    t.recordGet()
    wx.hideLoading()
  },
  getInt(ops){
    let t = this,userInfo=wx.getStorageSync('appUserInfo');
    t.setData({userInfo});
    app.args.request({
      url:'https://live.gdkktz.cn/shuidi/tasklist',
      data:{
        storeId:app.args.api.siteinfo.acid,
        share_type:ops.share_type || '',
        ori_token:ops.ori_token || '',
      },
      // url:'https://live.gdkktz.cn/shuidi/login',
      // data:{
      //   storeId:app.args.api.siteinfo.acid,
      // },
      success(e){
        t.setData({shuidicount:e.shuidicount, getList:e.data})
      }
    });
    // app.args.request({
    //   url:'https://live.gdkktz.cn/shuidi/paihang',
    //   data:{storeId:app.args.api.siteinfo.acid},
    //   success(e){
    //     t.setData({shareList:e.data})
    //   }
    // });
    app.args.request({
      url:'https://live.gdkktz.cn/shuidi/getcontent',
      success(e){
        console.log(e)
        e.content = e.content.replace(/\<img/gi, '<img style="width:100%;height:auto" ')
        t.setData({'guize':e.content})
      }
    });
  },
  conutClick(e) {
    let t=this,d = t.data,
        idx = e.currentTarget.dataset.idx,
        item = e.currentTarget.dataset.item,
        count = d.countList[idx];
    let animation = wx.createAnimation({duration:1000});
    //animation.translate(160-count.fix[1],250-count.fix[0]).opacity(0).step();
    //count.animation = animation;
    this.setData({countList:d.countList});
    wx.showLoading({title: '加载中',mask:true})
    app.args.request({
      url:'https://live.gdkktz.cn/shuidi/getshuidi',
      data:{storeId:app.args.api.siteinfo.acid, reward:item.reward, type:item.type, status:1},
      success(e){
        console.log(e)
        //t.setData({shuidicount:e.shuidicount})
        app.args.request({
          url:'https://live.gdkktz.cn/shuidi/tasklist',
          data:{
            storeId:app.args.api.siteinfo.acid,
          },
          // url:'https://live.gdkktz.cn/shuidi/login',
          // data:{
          //   storeId:app.args.api.siteinfo.acid,
          // },
          success(re){
            console.log(re)
            wx.hideLoading()
            t.setData({shuidicount:re.shuidicount, getList:re.data})
            wx.showToast({title: '签到成功',icon: 'none',duration: 1000})
          }
        })
      }
    });

  },
  goodsPage(){
    wx.navigateTo({url:'/activePages/trees/goods/goods?c='+this.data.shuidicount})
  },
  setShow(){
    this.setData({show:!this.data.show});
  },
  setShow2(){
    this.setData({show2:!this.data.show2});
  },
  /**
   * 规则弹出层
   */
  gzShow(){
    this.setData({
      gzShow:!this.data.gzShow
    });
  },
  /**
   * 获取记录
   */
  recordGet(){
    let t = this
    app.args.request({
      url:'https://live.gdkktz.cn/shuidi/buyrecord',
      data:{storeId:app.args.api.siteinfo.acid},
      success(e){
        t.setData({
          buyrecord:e.data,
        })
      }
    })
  },
  /**
   * 记录弹出层
   */
  recordShow(){
    let t = this
    if(!t.data.recordShow) {
      t.recordGet()
      if(t.data.buyrecord.length==0) {
        wx.showToast({title: '无记录',icon: 'none',duration: 2000})
        return;
      }
    }
    t.setData({recordShow:!t.data.recordShow})
  },
  /**
   * 查看核销二维码
   */
  onExtractQRCode(e) {
    let _this = this
    _this.setData({codeimg:_this.data.buyrecord[e.target.dataset.id].codeimg})
    _this.onToggleQRCodePopup()
  },
  /**
   * 核销码弹出层
   */
  onToggleQRCodePopup() {
    let _this = this;
    _this.setData({
      showQRCodePopup: !_this.data.showQRCodePopup
    })
    _this.recordShow()
  },
  onShareAppMessage(e){
    var item = this.data.getList[0]
    if(e.from === 'button'){
      // 按钮  // 右上角 menu
      item = e.target.dataset.item
    }
    return {
      path: '/activePages/trees/home/home?'+app.getShareUrlParams(),
      title: app.args.api.siteinfo.name,
    }
  }
});